package com.zmn.oms.services.impl.xno;


import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.model.entity.xno.OrderXnoBind;
import com.zmn.oms.model.entity.xno.OrderXnoBindQuery;
import com.zmn.oms.persistence.interfaces.xno.OrderXnoBindDao;
import com.zmn.oms.services.interfaces.orderencrypt.OrderEncryptService;
import com.zmn.oms.services.interfaces.xno.OrderXnoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：小号
 *
 * @author ligang
 * @date 2022/01/10 16:54
 */
@Service
public class OrderXnoServiceImpl implements OrderXnoService {

    @Resource
    private OrderXnoBindDao orderXnoBindDao;
    @Resource
    private OrderEncryptService orderEncryptService;

    @Override
    @Transactional
    public void saveXnoBind(OrderXnoBind orderXnoBind) {
        Date now = DateUtil.getNow();
        orderXnoBind.setCreateTime(now);
        orderXnoBind.setUpdateTime(now);
        if (Objects.nonNull(orderXnoBind.getBindTelephone())) {
            String encrypt = orderEncryptService.encrypt(orderXnoBind.getBindTelephone());
            orderXnoBind.setBindTelephone(encrypt);
        }
        orderXnoBindDao.insertOrderXnoBind(orderXnoBind);
    }

    @Override
    @Transactional
    public void updateOrderXnoBind(OrderXnoBind orderXnoBind) {
        orderXnoBind.setUpdateTime(DateUtil.getNow());
        if (Objects.nonNull(orderXnoBind.getBindTelephone())) {
            String encrypt = orderEncryptService.encrypt(orderXnoBind.getBindTelephone());
            orderXnoBind.setBindTelephone(encrypt);
        }
        orderXnoBindDao.updateOrderXnoBind(orderXnoBind);
    }

    @Override
    public Integer countXnoBindByQuery(OrderXnoBindQuery orderXnoBindQuery) {
        return orderXnoBindDao.countXnoBindByQuery(orderXnoBindQuery);
    }

    @Override
    public List<OrderXnoBind> queryOrderXnoBindByWorkIdList(List<Long> workIdList, int bindType) {
        List<OrderXnoBind> orderXnoBinds = orderXnoBindDao.queryOrderXnoBindByWorkIdList(workIdList, bindType);
        this.decryptTelephone(orderXnoBinds);
        return orderXnoBinds;
    }

    @Override
    public OrderXnoBind queryOrderXnoBindByQuery(OrderXnoBindQuery orderXnoBindQuery) {
        OrderXnoBind orderXnoBind = orderXnoBindDao.queryOrderXnoBindByQuery(orderXnoBindQuery);
        String decrypt = orderEncryptService.decrypt(orderXnoBind.getBindTelephone());
        orderXnoBind.setBindTelephone(decrypt);
        return orderXnoBind;
    }

    @Override
    public List<OrderXnoBind> queryOrderXnoBindListByQuery(OrderXnoBindQuery xnoBindQuery) {
        List<OrderXnoBind> orderXnoBinds = orderXnoBindDao.queryOrderXnoBindListByQuery(xnoBindQuery);
        this.decryptTelephone(orderXnoBinds);
        return orderXnoBinds;
    }

    /**
     * 解密绑定手机号
     *
     * @param orderXnoBinds
     * @return
     */
    private List<OrderXnoBind> decryptTelephone(List<OrderXnoBind> orderXnoBinds) {
        orderXnoBinds.stream().forEach(orderXnoBind -> {
            String decrypt = orderEncryptService.decrypt(orderXnoBind.getBindTelephone());
            orderXnoBind.setBindTelephone(decrypt);
        });
        return orderXnoBinds;
    }
}
